home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3536 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.5 KB  |  67 lines

  1. Path: news.compuserve.com!newsmaster
  2. From: Roberto Ortiz <74011.3205@compuserve.com>
  3. Newsgroups: comp.lang.c++
  4. Subject: Pointers to member functions HOW?
  5. Date: Wed, 24 Jan 1996 13:46:28 -0400
  6. Organization: CompuServe Incorporated
  7. Message-ID: <31067074.6B53@compuserve.com>
  8. NNTP-Posting-Host: dd60-130.compuserve.com
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0b4 (Win95; I)
  13.  
  14. I've been attempting for some time now to declare a pointer to a class' 
  15. memeber function. I have no problem with pointers to regular functions, but 
  16. with class members, I run into one of two problems:
  17.  
  18. a) I can't assign the function to the pointer.
  19. b) I can't call the pointer as a function.
  20.  
  21. whenever I solve one, I get the other.
  22.  
  23. Here's some example code in Borland C++:
  24.  
  25. #include <stdio.h>
  26.  
  27. void RegularFunction(int iVal)
  28. {
  29.     printf("[%d]\n", iVal);
  30. };
  31.  
  32. class TTest {
  33. public:
  34.     void MemberFunction(int iVal)
  35.     {
  36.         printf("[%d]\n", iVal);
  37.     };
  38. };
  39.  
  40. void main()
  41. {
  42.     typedef void (TFuncVoidInt)(int);
  43.  
  44.     TFuncVoidInt    *Func0;
  45.     TFuncVoidInt    *Func1;
  46.  
  47.     void (TTest::*Func2)(int);
  48.  
  49.     Func0 = RegularFunction;
  50.     Func1 = TTest::MemberFunction;
  51.     Func2 = &TTest::MemberFunction;
  52.  
  53.     Func0(100);
  54.     Func1(100);
  55.     Func2(100);
  56. };
  57.  
  58. with this code, I get the following compiler messages:
  59.  
  60. Compiling PTEST.CPP:
  61. Error PTEST.CPP 26: Cannot convert 'void (TTest::*)(int)' to 'void (*)(int)' 
  62. in function main()
  63. Error PTEST.CPP 31: Call of nonfunction in function main()
  64. function main()
  65.  
  66. Any suggestions?
  67.